package org.lys.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;

public class SvgToImg {
    public void convert2JPEG(String file) throws TranscoderException, IOException {
        InputStream in = new FileInputStream(file);
        JPEGTranscoder transcoder = new JPEGTranscoder();
        transcoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,
                 "org.apache.crimson.parser.XMLReaderImpl");
        transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(1.0));
        TranscoderInput input = new TranscoderInput(in);
        OutputStream ostream = new FileOutputStream(file.substring(0,file.lastIndexOf("."))+".jpg");
        TranscoderOutput output = new TranscoderOutput(ostream);
        transcoder.transcode(input, output);
        ostream.close();
        ostream.flush();
    }
    public void convert2PNG(String file) {
        try {
            PNGTranscoder t = new PNGTranscoder();
            InputStream in = new FileInputStream(file);
            TranscoderInput input = new TranscoderInput(in);
            FileOutputStream outputStream = new FileOutputStream(new File(
                    file.substring(0,file.lastIndexOf("."))+".png"));
            TranscoderOutput output = new TranscoderOutput(outputStream);
            t.transcode(input, output);
            outputStream.flush();
            outputStream.close();

         } catch (Exception ex) {
             ex.printStackTrace();
         }
    }
    
    public static void main(String[] args) throws Exception{
    	SvgToImg svgToImg = new SvgToImg();
//    	try {
			svgToImg.convert2JPEG("I:\\0.svg");
			svgToImg.convert2PNG("I:\\0.svg");
//		} catch (TranscoderException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
}
